package com.sgb.merchant.viewmodel;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.link.entity.UserInfo;
import com.sgb.link.entity.merchant.EnrolInfoEntiy;
import com.sgb.link.entity.merchant.InvestmentDetailEntity;
import com.sgb.link.router.PathMerchant;
import com.sgb.merchant.api.MerchantsNet;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.RouterKey;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.TransObjectUtils;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class InvestmentDetailViewModel extends BaseViewModel {
    public MutableLiveData<InvestmentDetailEntity> ldData = new MutableLiveData<>();
    public MutableLiveData<List<EnrolInfoEntiy>> ldList = new MutableLiveData<>();
    public MutableLiveData<BaseEntity> ldResult = new MutableLiveData<>();
    public MutableLiveData<UserInfo> ldUserInfo = new MutableLiveData<>();
    public MutableLiveData<Integer> ldState = new MutableLiveData<>();
    public MutableLiveData<Boolean> ldDel = new MutableLiveData<>();
    public MutableLiveData<Boolean> ldStatus = new MutableLiveData<>();
    public MutableLiveData<Boolean> ldUpdate = new MutableLiveData<>();

    public InvestmentDetailViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    /****
     * 获取详情
     * @param no 项目no
     */
    public void getDetils(Long no) {
        MerchantsNet.getInstance().getService().getInvestmentDetail(no).enqueue(new ICallBack<BaseEntity<InvestmentDetailEntity>>() {
            @Override
            public void onSuccess(BaseEntity<InvestmentDetailEntity> data) {
                if (data == null) {
                    return;
                }

                if (data.getData() != null) {
                    try {
                        InvestmentDetailEntity entity=data.getData();
                        TransObjectUtils.transObject(entity);
                        ldData.postValue(entity);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }

                }

            }

            @Override
            public void onFailureWithEntity(BaseEntity entity) {
                if (!TextUtils.isEmpty(entity.getMsg()) && entity.getMsg().equals("未找到该项目信息！")) {
                    ldState.postValue(1);
                } else {
                    ldState.postValue(-1);
                }
            }
        });
    }





    /**
     * 获取详情页并进行状态判断
     *
     * @param no
     */
    public void getDetailState(Activity context, Long no, int type, InvestmentDetailEntity entity) {//0 编辑 1上架 2下架 3删除
        DialogHelper.showProgressDialog(context, "加载中").setCanceledOnTouchOutside(true);
        MerchantsNet.getInstance().getService().getProjectState(no).enqueue(new ICallBack<BaseEntity<Integer>>() {
            @Override
            public void onSuccess(BaseEntity<Integer> data) {
                if (data.isSuccess()) {
                    int status = data.getData();//0已下架 1已删除 2已上架
                    if(status==1){
                        ldState.postValue(1);
                        return;
                    }
                    switch (type) {
                        case 0:
                            if (status != 0) {
                                ldStatus.postValue(false);
                            } else {
                                ARouter.getInstance().build(PathMerchant.InvestmentPublishActivity).withSerializable(RouterKey.DATA, entity)
                                        .withBoolean(RouterKey.BOOLEAN, true).navigation(context,3000);
                            }
                            break;
                        case 1:
                            if (status != 0) {
                                ldStatus.postValue(false);
                            } else {
                                new RoundDialog.Builder(context)
                                        .title("温馨提示")
                                        .content("确定上架该条信息？")
                                        .cancel("取消")
                                        .confirm("确定")
                                        .cancelable(false)
                                        .confirmCallback(v12 -> {
                                            updateInvestment(no, "0");
                                        }).show();
                            }
                            break;
                        case 2:
                            if (status !=2) {
                                ldStatus.postValue(false);
                            } else {
                                new RoundDialog.Builder(context)
                                        .title("温馨提示")
                                        .content("下架该信息后，前台将不再继续展示，确定下架？")
                                        .cancel("取消")
                                        .confirm("确定")
                                        .cancelable(false)
                                        .confirmCallback(v12 -> {
                                            updateInvestment(no, "1");
                                        }).show();
                            }
                            break;
                        case 3:
                            if (status != 0) {
                                ldStatus.postValue(false);
                            } else {
                                new RoundDialog.Builder(context)
                                        .title("温馨提示")
                                        .content("请问是否确认删除？")
                                        .cancel("取消")
                                        .confirm("确定")
                                        .cancelable(false)
                                        .confirmCallback(v12 -> {
                                            delete(no);
                                        }).show();
                            }
                            break;
                    }


                }
            }
        });
    }

    /**
     * 上架下架操作
     *
     * @param no
     * @param status 0上架 1下架
     */
    public void updateInvestment(long no, String status) {
        MerchantsNet.getInstance().getService().updateInvestment(no, status).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data.isSuccess()) {
                    ldUpdate.postValue(true);
                }
            }
        });
    }

    /**
     * 删除投资项目
     *
     * @param no
     */
    public void delete(Long no) {
        MerchantsNet.getInstance().getService().delProject(no).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data.isSuccess()) {
                    ldDel.postValue(true);
                }
            }
        });
    }

    /**
     * 获取报名列表
     *
     * @param no 项目no
     */
    public void getEnrolList(Long no) {
        MerchantsNet.getInstance().getService().getEnrolList(no, 1, 5).enqueue(new Callback<BaseEntity<ListResponse<List<EnrolInfoEntiy>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<EnrolInfoEntiy>>>> call, Response<BaseEntity<ListResponse<List<EnrolInfoEntiy>>>> response) {
                if (response.body() == null) {
                    return;
                }
                if (response.body() != null && response.body().isSuccess()) {
                    if (response.body().getData() != null) {
                        ldList.postValue(response.body().getData().getList());
                    }
                } else {
                    MToast.showToast(getApplication(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<EnrolInfoEntiy>>>> call, Throwable t) {

            }
        });
    }

    /**
     * 获取交流IM用户信息
     *
     * @param no
     */
    public void getImInfo(String no) {
        MerchantsNet.getInstance().getService().getImInfo(no).enqueue(new Callback<BaseEntity<UserInfo>>() {
            @Override
            public void onResponse(Call<BaseEntity<UserInfo>> call, Response<BaseEntity<UserInfo>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body() != null && response.body().isSuccess() && response.body().getData() != null) {
                    ldUserInfo.postValue(response.body().getData());
                } else {
                    MToast.showToast(getApplication(), response.body().getMsg());
                }
            }


            @Override
            public void onFailure(Call<BaseEntity<UserInfo>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 关注投资项目
     *
     * @param no
     */
    public void follow(long no) {
        MerchantsNet.getInstance().getService().follow(no, "FT1").enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body() != null && response.body().isSuccess()) {
                    MToast.showToast(getApplication(), response.body().getMsg());
                    ldResult.postValue(response.body());
                } else {
                    MToast.showToast(getApplication(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 获取投资项目下架删除状态
     *
     * @param no
     */
    public void getState(Long no) {
        MerchantsNet.getInstance().getService().getProjectState(no).enqueue(new Callback<BaseEntity<Integer>>() {
            @Override
            public void onResponse(Call<BaseEntity<Integer>> call, Response<BaseEntity<Integer>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().isSuccess()) {
                    ldState.postValue(response.body().getData());
                } else {
                    MToast.showToast(getApplication(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<Integer>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
